package com.tengfei.api.exam;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.common.utils.poi.ExcelUtil;
import com.tengfei.main.exam.domain.bo.PaperUserQuBo;
import com.tengfei.main.exam.domain.bo.UserRecodeBo;
import com.tengfei.main.exam.domain.export.RecodeExportVo;
import com.tengfei.main.exam.domain.vo.PaperVo;
import com.tengfei.main.exam.domain.vo.UserRecordVo;
import com.tengfei.main.exam.service.IRecodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @author feige
 * @version v1.0
 * @date 2022-09-12-16:59
 * @description
 */

@Validated
@Api(value = "考试记录控制器", tags = {"考试记录管理"})
@RestController
@RequestMapping("/exam/recode")
public class RecodeController extends BaseController {

    @Resource
    private IRecodeService iRecodeService;

    /**
     * 查询试卷列表
     */
    @ApiOperation("查询试卷列表")
    @SaCheckPermission("exam:record:list")
    @GetMapping("/list/paper")
    public R<List<PaperVo>> list(@ApiParam("试卷名称") @RequestParam(value = "paperName", required = false) String paperName,
                                 @ApiParam("数量(默认10)") @RequestParam(value = "num", defaultValue = "10") Integer num
    ) {
        return R.ok(iRecodeService.queryPaperList(paperName, num));
    }

    /**
     * 查询某门考试下用户的考试成绩
     */
    @ApiOperation("查询某门考试下用户的考试成绩")
    @SaCheckPermission("exam:record:list")
    @GetMapping("/list")
    public TableDataInfo<UserRecordVo> paperUserAnsRecodeList(UserRecodeBo bo, PageQuery pageQuery) {
        return iRecodeService.queryUserRecordPage(bo, pageQuery);
    }

    /**
     * 导出用户考试成绩列表
     */
    @ApiOperation("导出用户考试成绩列表")
    @SaCheckPermission("exam:record:export")
    @Log(title = "导出用户考试成绩列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserRecodeBo bo, HttpServletResponse response) {
        List<UserRecordVo> list = iRecodeService.exportUserRecordList(bo);
        List<RecodeExportVo> recodeExportVos = BeanUtil.copyToList(list, RecodeExportVo.class);
        ExcelUtil.exportExcel(recodeExportVos, "用户考试记录", RecodeExportVo.class, response);
    }

    /**
     * 查询用户试卷题目详细信息
     *
     * @param userId
     * @param paperId
     * @return
     */
    @ApiOperation("查询用户试卷答题记录列表")
    @SaCheckPermission("exam:recode:list")
    @GetMapping("/list/quAns")
    public R<List> userAnsList(@ApiParam(value = "用户id", required = true) @NotNull(message = "用户id不能为空")
                               @RequestParam("userId") Long userId,
                               @ApiParam(value = "试卷id", required = true) @NotNull(message = "试卷id不能为空")
                               @RequestParam("paperId") Long paperId) {
        return R.ok(iRecodeService.userAnsList(userId, paperId));
    }


    @ApiOperation("更改用户已答题目分数")
    @SaCheckPermission("exam:recode:edit")
    @Log(title = "更改用户已答题目分数", businessType = BusinessType.UPDATE)
    @PutMapping("/user/changeScore")
    public R<Void> changeUserScore(@RequestBody PaperUserQuBo bo) {
        return toAjax(iRecodeService.updateUserScore(bo));
    }

    /**
     * 查询当前试卷用户排名
     *
     * @param paperId
     * @return
     */
    @ApiOperation("查询当前试卷用户排名")
    @SaCheckPermission("exam:recode:list")
    @GetMapping("/list/userRank")
    public R<List<Map<String, Object>>> userRanking(@ApiParam(value = "试卷id", required = true) @NotNull(message = "试卷id不能为空")
                                                    @RequestParam("paperId") Long paperId) {
        return R.ok(iRecodeService.userRanking(paperId));
    }

    /**
     * 查询当前试卷统计信息
     *
     * @param paperId
     * @return
     */
    @ApiOperation("查询当前试卷统计信息")
    @SaCheckPermission("exam:recode:list")
    @GetMapping("/paper/census")
    public R<Map<String, Object>> paperCensus(@ApiParam(value = "试卷id", required = true) @NotNull(message = "试卷id不能为空")
                                              @RequestParam("paperId") Long paperId) {

        return R.ok(iRecodeService.paperCensus(paperId));

    }

}
